<?php
// +----------------------------------------------------------------------
// | 多客怪兽框架多平台开发框架 [ 以下开发工具自动生成-版权勿删 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021~2024 http://www.dkyx.cc All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 刘泓宾（514660094@qq.com）
// +----------------------------------------------------------------------

namespace addons\cloud_market\controller\admin;

use addons\cloud_market\model\CloudMarketSiteAddon;
use app\common\services\table\Column;
use app\common\services\table\DataForm;
use app\common\services\table\DataTable;
use hiduoke\controller\AddonsController;
use hiduoke\services\FormBuilder as Form;

/**
 * 站点应用控制器
 * Create by :  刘泓宾<514660094@qq.com>
 * Copyright :  copyright (c) 多客怪兽, www.dkyx.cc
 */
class SiteAddon extends AddonsController
{
    /**
     * @var CloudMarketSiteAddon
     */
    protected $model;

    // 设置关联查询
    protected array $withJoinTable = ['site','addon'];

    // 设置快捷搜索字段
    protected string|array $quickSearchField =  [];

    /**
     * 控制器初始化
     * Create by :  刘泓宾<514660094@qq.com>
     * @return void
     */
    public function initialize(): void
    {
        parent::initialize();
        $this->model = new CloudMarketSiteAddon();
    }


    /**
     * 获取数据表格结构
     * Create by :  刘泓宾<514660094@qq.com>
     * @return \think\response\Json
     */
    public function table()
    {
        $DataTable = (new DataTable($this->model));
        if ($this->request->param('recycle')) {
            // 回收站
            $DataTable->field([$this->model->getPk(), 'delete_time']);
        }
        $site_button = Column::make()->demoButton;
        $site_button['title'] = '授权列表';
        $site_button['text'] = '授权列表';
        $site_button['name'] = 'site_button';
        $column = $DataTable
        // 设置 站点ID (site_id) 字段自定义配置
        ->setField("site_id",["label"=>"站点名称","prop"=>"site.title","comSearchProp"=>"site_id","type"=>"remoteSelect"])
        // 设置 应用ID (addon_id) 字段自定义配置
        ->setField("addon_id",["label"=>"应用名","prop"=>"addon.title","comSearchProp"=>"addon_id","type"=>"remoteSelect"])
        ->setRemoteUrls(["site_id"=>"/cloud_market/admin.site?label=title&val=id","addon_id"=>"/cloud_market/admin.addon?label=title&val=id"])
        // 自定义操作列
        ->operation(
            Column::make()->prop('operation')
                // 操作列标题
                ->label('操作')
                ->render('buttons')
                // 宽度
                ->width(180)
                ->other('fixed', 'right')
                // 禁用搜索
                ->operator(false)
                // 自定义按钮组
                ->buttons([$site_button,"edit","del"])
                ->create()
        )->create();

        return $this->success('ok', ['column' => $column, 'pk' => $this->model->getPk()]);
    }

    /**
     * 获取表单配置
     * @param $id
     * @return \think\response\Json
     * @throws \FormBuilder\Exception\FormBuilderException
     */
    public function form()
    {
        $dataForm = new DataForm($this->model);
        $form = $dataForm
        // 设置 站点ID (site_id) 字段自定义配置
        ->setField("site_id",["Title"=>"站点名称","render"=>"remoteSelect"])
        // 设置 应用ID (addon_id) 字段自定义配置
        ->setField("addon_id",["Title"=>"应用名","render"=>"remoteSelect"])
        ->setRemoteUrls(["site_id"=>"/cloud_market/admin.site?label=title&val=id","addon_id"=>"/cloud_market/admin.addon?label=title&val=id"])
        ->create();
        return $this->success('', $form);
    }


}
